package com.alex.xmlyfm.fragments.discover;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.alex.xmlyfm.Constants;
import com.alex.xmlyfm.R;
import com.alex.xmlyfm.fragments.BaseFragment;
import com.alex.xmlyfm.tasks.DiscoverCategoryTask;
import com.alex.mylibrary.tasks.TaskCallBack;
import com.alex.mylibrary.tasks.TaskResult;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverCategoryFragment extends BaseFragment implements TaskCallBack {


    public DiscoverCategoryFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DiscoverCategoryTask task = new DiscoverCategoryTask(this);

        task.execute();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_sort, container, false);

        ListView listView = (ListView) ret.findViewById(R.id.test_list);
        ArrayList<String> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add(i+"");
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,data);
        listView.setAdapter(adapter);

        return ret;

    }


    @Override
    public String getFragmentTitle() {
        return "分类";
    }

    /**
     * 异步任务读取杰森
     * @param result 返回的结果
     */
    @Override
    public void onTaskFinished(TaskResult result) {
        if (result != null) {
            int action = result.action;
            if (action == Constants.TASK_ACTION_CATEGORIES){
                // TODO 结果是从 发现 分类 任务中返回的，获取的时分类

                if (result.resultCode == Constants.TASK_RESULT_OK){
                    // TODO 加载成功
                    Object data = result.data;
                    if (data != null && data instanceof List) {
                        List list = (List) data;
                        Log.d("Category","List = " + list);
                    }
                }else{
                    // TODO 加载失败

                }

            }


        }
    }
}
